package com.tinyengine.it.service.db.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tinyengine.it.common.utils.JavaClassNameValidator;
import com.tinyengine.it.mapper.DbTableFieldsInfoMapper;
import com.tinyengine.it.mapper.DbTableInfoMapper;
import com.tinyengine.it.model.entity.DbTableFieldsInfo;
import com.tinyengine.it.model.entity.DbTableInfo;
import com.tinyengine.it.model.entity.SysTermCache;
import com.tinyengine.it.model.vo.DbTableFieldsInfoVo;
import com.tinyengine.it.model.vo.DbTableInfoVo;
import com.tinyengine.it.service.db.DbTableInfoService;
import com.tinyengine.it.service.db.SysTermCacheService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @description 针对表【i_db_table_info】的数据库操作Service实现
 * @createDate 2025-03-15 10:39:22
 */
@Service
@RequiredArgsConstructor
public class DbTableInfoServiceImpl extends ServiceImpl<DbTableInfoMapper, DbTableInfo> implements DbTableInfoService {

    private final DbTableInfoMapper dbTableInfoMapper;
    private final DbTableFieldsInfoMapper dbTableFieldsInfoMapper;
    private final SysTermCacheService sysTermCacheService;

    @Override
    @Transactional
    public DbTableInfoVo saveInfo(DbTableInfoVo dbTableInfoVo) {
        DbTableInfo dbTableInfo = BeanUtil.copyProperties(dbTableInfoVo, DbTableInfo.class);

        String validClassName = JavaClassNameValidator.isValidClassName(dbTableInfo.getName());
        if (StrUtil.isNotBlank(validClassName)) {
            throw new RuntimeException(validClassName);
        }
        dbTableInfoMapper.insertOrUpdate(dbTableInfo);

        List<DbTableFieldsInfo> dbTableFieldsInfo = BeanUtil.copyToList(dbTableInfoVo.getFields(), DbTableFieldsInfo.class);
        dbTableFieldsInfo.forEach(e -> {
            e.setTableId(dbTableInfo.getId());
            String fieldValid = JavaClassNameValidator.isValidClassName(e.getFieldCode());
            if (StrUtil.isNotBlank(fieldValid)) {
                throw new RuntimeException(fieldValid);
            }
        });

        List<Integer> fields = dbTableFieldsInfo.stream().filter(f -> ObjUtil.isNotNull(f.getId())).map(m -> m.getId()).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(fields) && fields.size() > 0) {
            dbTableFieldsInfoMapper.delete(Wrappers.<DbTableFieldsInfo>lambdaQuery().eq(DbTableFieldsInfo::getTableId, dbTableInfo.getId()).notIn(DbTableFieldsInfo::getId, fields));
        } else {
            dbTableFieldsInfoMapper.delete(Wrappers.<DbTableFieldsInfo>lambdaQuery().eq(DbTableFieldsInfo::getTableId, dbTableInfo.getId()));
        }
        dbTableFieldsInfoMapper.insertOrUpdate(dbTableFieldsInfo);

        //跟新系统名称缓存库
        List<SysTermCache> sysTermCaches = dbTableFieldsInfo.stream().map(m -> new SysTermCache().setName(m.getNameCh()).setEnName(StrUtil.toCamelCase(m.getFieldCode()))).collect(Collectors.toList());
        sysTermCacheService.updateSysTermCacheInfo(sysTermCaches);

        //将所有数据包含新增后的Id返回
        List<DbTableFieldsInfoVo> infoVos = BeanUtil.copyToList(dbTableFieldsInfo, DbTableFieldsInfoVo.class);
        DbTableInfoVo tableInfoVo = BeanUtil.copyProperties(dbTableInfo, DbTableInfoVo.class);
        tableInfoVo.setFields(infoVos);

        return tableInfoVo;
    }
}




